# -*- coding: utf-8 -*-
# @Time    : 2019/8/24 15:50
# @Author  : XuHaoTang
# @File    : stocks_class.py
import tushare as ts
import pymongo
from mongo_config import *

mongo_client = pymongo.MongoClient(MONGO_URL, 27017)
db = mongo_client['stocks_class']

dbl = mongo_client['stock_data']
# 概念
def get_concept():
    concept = ts.get_concept_classified()
    dicts = {}
    for index, datum in concept.iterrows():
        dicts[datum['code']] = datum['c_name']
    return dicts

# 区域
def get_area():
    concept = ts.get_area_classified()
    dicts = {}
    for index, datum in concept.iterrows():
        dicts[datum['code']] = datum['area']
    return dicts

# 价格
def getprice():
    dicts = {}
    alist = dbl['details'].find({}, {'_id': 1, 'trade': 1})
    for a in alist:
        dicts[a['_id']] = a['trade']
    return dicts

# 行业
def get_indus():
    indus = ts.get_industry_classified()
    concept = get_concept()
    area = get_area()
    prices = getprice()
    # print(prices)
    stks = []
    for index, datum in indus.iterrows():
        # print(datum)
        dicts = {}
        dicts['_id'] = datum['code']
        dicts['name'] = datum['name']
        # 行业
        dicts['c_name'] = datum['c_name']
        # 概念
        try:
            dicts['concept'] = concept[datum['code']]
            # 地域
            dicts['area'] = area[datum['code']]
            dicts['price'] = prices[datum['code']]
            dicts['date'] = '2019-08-23'
        except:
            pass
        if len(dicts) == 7:
            stks.append(dicts)
            # print(dicts)
    return stks

if __name__ == '__main__':
    # stks = get_indus()
    # print(stks)
    # db['class'].insert_many(stks, ordered=False)
    hs300 = ts.get_hs300s()
    hslist = []
    for index, datum in hs300.iterrows():
        print(datum['code'], ': ', datum['name'])
        hslist.append(datum['code'])
    print(hslist)
'''targetres收益数据'''
test = [
 {'code': '600031', 'weight': 0.2, 'profit_day': -0.0190336749633968, 'profit_week': 0.0105580693815988,
  'profit_month': -0.0373563218390806, 'profit_year': 0.483942414174972,
  'profit_all': [0.673880597014925, 0.68955223880597, 0.688059701492537, 0.68955223880597, 0.666417910447761,
                 0.671641791044776, 0.66865671641791, 0.675373134328358, 0.658208955223881, 0.651492537313433,
                 0.645522388059702, 0.641791044776119, 0.643283582089552, 0.641791044776119, 0.627611940298507,
                 0.611940298507463, 0.616417910447761, 0.633582089552239, 0.640298507462687, 0.647761194029851,
                 0.658208955223881, 0.647761194029851, 0.664925373134328, 0.664925373134328, 0.662686567164179,
                 0.652985074626866, 0.633582089552239, 0.638805970149254, 0.613432835820896, 0.617910447761194,
                 0.62089552238806, 0.608955223880597, 0.612686567164179, 0.574626865671642, 0.584328358208955,
                 0.61044776119403, 0.591791044776119, 0.597761194029851, 0.593283582089552, 0.588805970149254,
                 0.569402985074627, 0.575373134328358, 0.587313432835821, 0.591044776119403, 0.607462686567164,
                 0.602238805970149, 0.607462686567164, 0.604477611940298, 0.601492537313433, 0.598507462686567,
                 0.611940298507463, 0.611194029850746, 0.614925373134328, 0.614925373134328, 0.609701492537313,
                 0.608955223880597, 0.596268656716418, 0.593283582089552, 0.593283582089552, 0.58134328358209,
                 0.582089552238806, 0.585820895522388, 0.594776119402985, 0.592537313432836, 0.599253731343284,
                 0.616417910447761, 0.614925373134328, 0.611194029850746, 0.603731343283582, 0.611194029850746,
                 0.608955223880597, 0.612686567164179, 0.623134328358209, 0.635074626865672, 0.63134328358209,
                 0.647014925373134, 0.641791044776119, 0.63955223880597, 0.633582089552239, 0.630597014925373,
                 0.638805970149254, 0.621641791044776, 0.626865671641791, 0.626865671641791, 0.622388059701492,
                 0.600746268656716, 0.591791044776119, 0.602238805970149, 0.607462686567164, 0.604477611940298,
                 0.605223880597015, 0.6, 0.611194029850746, 0.625373134328358, 0.62910447761194, 0.632089552238806,
                 0.636567164179104, 0.644776119402985, 0.642537313432836, 0.637313432835821, 0.65, 0.661194029850746,
                 0.676865671641791, 0.677611940298507, 0.682089552238806, 0.680597014925373, 0.680597014925373,
                 0.707462686567164, 0.707462686567164, 0.723880597014925, 0.728358208955224, 0.727611940298507,
                 0.705970149253731, 0.73955223880597, 0.717164179104478, 0.723134328358209, 0.727611940298507,
                 0.773134328358209, 0.814925373134328, 0.805970149253731, 0.776119402985075, 0.775373134328358, 0.8,
                 0.861194029850746, 0.856716417910448, 0.863432835820895, 0.837313432835821, 0.826865671641791,
                 0.834328358208955, 0.877611940298507, 0.858955223880597, 0.870149253731343, 0.923880597014925,
                 0.969402985074627, 0.955223880597015, 0.977611940298507, 0.97910447761194, 0.96044776119403,
                 0.91865671641791, 0.902238805970149, 0.917910447761194, 0.9, 0.953731343283582, 0.966417910447761,
                 0.942537313432836, 0.964179104477612, 0.997014925373134, 1.02985074626866, 1.01194029850746,
                 1.04477611940298, 0.983582089552239, 0.96865671641791, 0.96044776119403, 0.986567164179105,
                 0.978358208955224, 0.982089552238806, 0.979850746268657, 0.909701492537313, 0.917164179104478,
                 0.920149253731343, 0.886567164179105, 0.88955223880597, 0.917910447761194, 0.914179104477612,
                 0.850746268656716, 0.904477611940298, 0.884328358208955, 0.885074626865672, 0.92910447761194,
                 0.911194029850746, 0.892537313432836, 0.905970149253731, 0.906716417910448, 0.873134328358209,
                 0.864925373134328, 0.887313432835821, 0.891791044776119, 0.888059701492537, 0.897761194029851,
                 0.91865671641791, 0.944029850746269, 0.938805970149254, 0.921641791044776, 0.900746268656716,
                 0.892537313432836, 0.893283582089552, 0.902238805970149, 0.891791044776119, 0.911194029850746,
                 0.964179104477612, 0.962686567164179, 0.966417910447761, 0.954477611940298, 0.951492537313433,
                 0.961194029850746, 0.962686567164179, 0.983582089552239, 0.979850746268657, 0.983582089552239,
                 0.973880597014925, 0.966417910447761, 0.966417910447761, 0.976119402985075, 1.0, 0.984328358208955,
                 0.964179104477612, 0.959701492537313, 0.96044776119403, 0.947761194029851, 0.945522388059701,
                 0.93134328358209, 0.956716417910448, 0.979850746268657, 1.00223880597015, 1.00149253731343,
                 1.00298507462687, 1.02313432835821, 1.01567164179104, 1.02313432835821, 1.01492537313433,
                 1.03880597014925, 1.04402985074627, 1.09328358208955, 1.07014925373134, 1.06791044776119,
                 1.05373134328358, 1.0589552238806, 1.03955223880597, 1.01417910447761, 1.01194029850746,
                 1.0134328358209, 1.01194029850746, 1.00597014925373, 1.02313432835821, 0.98955223880597,
                 0.978358208955224, 0.992537313432836, 0.98955223880597, 1.01194029850746, 1.0410447761194,
                 1.01940298507463, 1.0]},
 {'code': '601989', 'weight': 0.0285714285714286, 'profit_day': 0.00337268128161905, 'profit_week': 0.0136286201022147,
  'profit_month': 0.0223367697594503, 'profit_year': 0.461916461916462,
  'profit_all': [0.684033613445378, 0.695798319327731, 0.699159663865546, 0.702521008403361, 0.697478991596639,
                 0.694117647058824, 0.699159663865546, 0.717647058823529, 0.705882352941177, 0.704201680672269,
                 0.699159663865546, 0.697478991596639, 0.700840336134454, 0.702521008403361, 0.715966386554622,
                 0.710924369747899, 0.704201680672269, 0.717647058823529, 0.721008403361345, 0.714285714285714,
                 0.722689075630252, 0.722689075630252, 0.722689075630252, 0.710924369747899, 0.714285714285714,
                 0.692436974789916, 0.687394957983193, 0.695798319327731, 0.66218487394958, 0.66890756302521,
                 0.665546218487395, 0.66890756302521, 0.677310924369748, 0.665546218487395, 0.673949579831933,
                 0.699159663865546, 0.689075630252101, 0.687394957983193, 0.68235294117647, 0.689075630252101,
                 0.692436974789916, 0.692436974789916, 0.700840336134454, 0.697478991596639, 0.705882352941177,
                 0.704201680672269, 0.707563025210084, 0.702521008403361, 0.694117647058824, 0.694117647058824,
                 0.702521008403361, 0.710924369747899, 0.705882352941177, 0.714285714285714, 0.715966386554622,
                 0.719327731092437, 0.709243697478992, 0.709243697478992, 0.715966386554622, 0.692436974789916,
                 0.694117647058824, 0.705882352941177, 0.707563025210084, 0.702521008403361, 0.712605042016807,
                 0.722689075630252, 0.721008403361345, 0.717647058823529, 0.712605042016807, 0.715966386554622,
                 0.712605042016807, 0.714285714285714, 0.715966386554622, 0.721008403361345, 0.707563025210084,
                 0.710924369747899, 0.710924369747899, 0.704201680672269, 0.697478991596639, 0.685714285714286,
                 0.709243697478992, 0.705882352941177, 0.702521008403361, 0.715966386554622, 0.714285714285714,
                 0.715966386554622, 0.73109243697479, 0.757983193277311, 0.778151260504202, 0.761344537815126,
                 0.751260504201681, 0.747899159663866, 0.752941176470588, 0.757983193277311, 0.756302521008403,
                 0.736134453781513, 0.73781512605042, 0.746218487394958, 0.741176470588235, 0.741176470588235,
                 0.73781512605042, 0.74453781512605, 0.741176470588235, 0.739495798319328, 0.729411764705882,
                 0.727731092436975, 0.722689075630252, 0.734453781512605, 0.741176470588235, 0.747899159663866,
                 0.763025210084034, 0.773109243697479, 0.763025210084034, 0.779831932773109, 0.789915966386555,
                 0.805042016806723, 0.776470588235294, 0.79327731092437, 0.852100840336134, 0.860504201680672,
                 0.884033613445378, 0.87563025210084, 0.895798319327731, 0.932773109243697, 0.934453781512605,
                 0.927731092436975, 0.907563025210084, 0.857142857142857, 0.880672268907563, 0.897478991596639,
                 0.877310924369748, 0.86218487394958, 0.873949579831933, 0.904201680672269, 0.899159663865546,
                 0.894117647058824, 0.897478991596639, 0.894117647058824, 0.976470588235294, 0.921008403361345,
                 0.915966386554622, 0.915966386554622, 0.981512605042017, 1.07899159663866, 1.08739495798319,
                 1.09915966386555, 1.11764705882353, 1.13109243697479, 1.10084033613445, 1.09747899159664,
                 1.06218487394958, 1.06218487394958, 1.06386554621849, 1.08571428571429, 1.09075630252101,
                 1.13445378151261, 1.12436974789916, 1.09411764705882, 1.03697478991597, 1.02857142857143,
                 0.989915966386555, 0.969747899159664, 0.929411764705882, 0.952941176470588, 0.860504201680672,
                 0.86890756302521, 0.86890756302521, 0.858823529411765, 0.887394957983193, 0.870588235294118,
                 0.863865546218487, 0.872268907563025, 0.878991596638656, 0.848739495798319, 0.853781512605042,
                 0.86890756302521, 0.857142857142857, 0.845378151260504, 0.836974789915966, 0.857142857142857,
                 0.867226890756303, 0.87563025210084, 0.863865546218487, 0.86218487394958, 0.86218487394958,
                 0.847058823529412, 0.845378151260504, 0.840336134453782, 0.840336134453782, 0.865546218487395,
                 0.85546218487395, 0.853781512605042, 0.889075630252101, 0.882352941176471, 0.87563025210084,
                 0.884033613445378, 0.902521008403361, 0.912605042016807, 0.905882352941176, 0.907563025210084,
                 0.939495798319328, 0.927731092436975, 0.934453781512605, 0.942857142857143, 1.01344537815126,
                 1.09075630252101, 1.08403361344538, 1.05882352941176, 1.02352941176471, 1.02689075630252,
                 1.01008403361345, 1.0, 0.994957983193277, 1.03025210084034, 1.02352941176471, 1.02689075630252, 1.0,
                 1.0, 0.961344537815126, 0.973109243697479, 0.978151260504202, 0.986554621848739, 1.0, 1.01176470588235,
                 1.01008403361345, 1.00840336134454, 0.998319327731092, 0.966386554621849, 0.957983193277311,
                 0.92436974789916, 0.907563025210084, 0.994957983193277, 0.968067226890756, 0.978151260504202,
                 0.989915966386555, 1.02689075630252, 1.00672268907563, 0.986554621848739, 0.998319327731092,
                 1.01176470588235, 0.996638655462185, 1.0]}
]
'''
# 夏普率、回测计算
import numpy as np
def Cacl(dline, HSI, champion_x):
    closeall = np.dot(dline, champion_x) / HSI
    dayr = (closeall[1:] - closeall[:-1]) / closeall[:-1]
    exre = dayr - 0.04 / 250
    sharp = np.sqrt(250) * np.mean(exre) / np.std(exre)
    lasthigh = closeall[0]
    rollback = 0
    for i in range(1, closeall.shape[0]):
        if closeall[i] > lasthigh:
            lasthigh = closeall[i]
        else:
            rb = (lasthigh - closeall[i]) / lasthigh
            if rb > rollback:
                rollback = rb

/* page: 页码；pagesize: 每页的数量 */
let page = req.body.page;
let pagesize = req.body.pagesize;
let queryResult = collection.find(queryCondition).limit(pageSize).skip((page - 1) * pageSize).sort({'_id': -1});
queryResult.exec((err, value) => {
  if(err) {
    reject(err);
  } else {
    resolve({total, value});
  }
})
'''
